<?php
App::uses('AppController', 'Controller');
/**
 * Users Controller
 *
 * @property User $User
 */
class UsersController extends AppController {

public function beforeFilter() {
	parent::beforeFilter();
	$this->Auth->allow('add'); // Letting users register themselves
}

public function login() {
	if ($this->request->is('post')) {
		if ($this->Auth->login()) {
			$this->redirect($this->Auth->redirect());
		} else {
			$this->Session->setFlash(__('Invalid username or password, try again'));
		}
	}
}

public function logout() {
	$this->redirect($this->Auth->logout());
}

public function isAuthorized($user) {
    // Admin can access every action
     if (isset($user['Role']['name']) && $user['Role']['name'] !== 'Admin') {
        if($this->action=='index') {
		$this->redirect(array('action' => 'view',$user['id']));
	}
	return true;
     }

    // Default deny
    return parent::isAuthorized($user);
}

/**
 * index method
 *
 * @return void
 */
	public function index() {
		$this->User->recursive = 0;
		$this->set('users', $this->paginate());
	}

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		$this->User->id = $id;
		if (!$this->User->exists()) {
			throw new NotFoundException(__('Invalid user'));
		}
		$this->set('user', $this->User->read(null, $id));
	}

/**
 * add method
 *
 * @return void
 */
	public function add() {
		if ($this->request->is('post')) {
			$this->User->create();
			if ($this->User->save($this->request->data)) {
				$objUsuario = $this->User->read();
				
				$this->User->UserDatum->create();
				$con_el_id = array_merge_recursive($this->request->data,array("UserDatum"=>array('user_id'=>$objUsuario['User']['id'])));
				
				if(!$this->User->UserDatum->save($con_el_id)){
					$this->User->delete();
					$this->Session->setFlash(__('The user could not be saved. Please, try again.'));
				}
				else {
					$this->Session->setFlash(__('The user has been saved'));
					$this->redirect(array('action' => 'index'));
				}
			} else {
				$this->Session->setFlash(__('The user could not be saved. Please, try again.'));
			}
		}
		$roles = $this->User->Role->find('list',array('conditions' => array('name' => 'User')));
		$this->set(compact('roles'));
	}

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
		$this->User->id = $id;
		if (!$this->User->exists()) {
			throw new NotFoundException(__('Invalid user'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			if ($this->User->save($this->request->data)) {
				
				$objUserDatum = $this->User->UserDatum->find('first',array('conditions' => array('user_id' => $id)));
				
				$this->User->UserDatum->read(null,$objUserDatum['UserDatum']["id"]);
				
				if (!$this->User->UserDatum->save($this->request->data)) {
					///NO SE SI VALGA LA PENA TENER UNA COPIA PARA RESTAURAR EL USUARIO
					$this->Session->setFlash(__('The user could not be saved. Please, try again.'));
				}
				else {
					$this->Session->setFlash(__('The user has been saved'));
					$this->redirect(array('action' => 'index'));
				}
			} else {
				$this->Session->setFlash(__('The user could not be saved. Please, try again.'));
			}
		} else {
			$this->request->data = $this->User->read(null, $id);
		}
		$roles = $this->User->Role->find('list');
		$this->set(compact('roles'));
	}

/**
 * delete method
 *
 * @throws MethodNotAllowedException
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
		if (!$this->request->is('post')) {
			throw new MethodNotAllowedException();
		}
		$this->User->id = $id;
		if (!$this->User->exists()) {
			throw new NotFoundException(__('Invalid user'));
		}
		else {
			$objUserDatum = $this->User->UserDatum->find('first',array('conditions' => array('user_id' => $id)));
			$this->User->UserDatum->read(null,$objUserDatum['UserDatum']["id"]);
			
			if ($this->User->UserDatum->delete()) {
				if ($this->User->delete()) {
					$this->Session->setFlash(__('User deleted'));
					$this->redirect(array('action' => 'index'));
				}
			}
		}
		$this->Session->setFlash(__('User was not deleted'));
		$this->redirect(array('action' => 'index'));
	}
}
